package com.cmyshare.design.strategyAndFactory;

/**
 * @version 1.0
 * @Author cmy
 * @Date 2025/10/13 14:20
 * @desc 支付上下文类
 */
// 支付上下文类
public class PaymentContext {
    private PaymentStrategy paymentStrategy;

    // 构造方法，通过支付类型初始化策略
    public PaymentContext(String paymentType) {
        this.paymentStrategy = PaymentFactory.getPaymentStrategy(paymentType);
    }

    // 执行支付
    public void executePayment(double amount) {
        paymentStrategy.pay(amount);
    }

    // 切换支付方式
    public void setPaymentStrategy(String paymentType) {
        this.paymentStrategy = PaymentFactory.getPaymentStrategy(paymentType);
    }
}
